package com.twinkle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twinkle.dao.DataBaseMapper;
import com.twinkle.model.DataBase;
import com.twinkle.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @title: DataSourceServiceImpl
 * @description:
 * @author: Paul
 * @date: 2023/2/2 15:22
 */
@Service
public class DataBaseServiceImpl {

    @Autowired
    private DataBaseMapper dataBaseMapper;


    //register
    public synchronized int  register(DataBase dataBase) throws Exception {
        if (isEmpty(dataBase.getName())) {
            return dataBaseMapper.insert(dataBase);
        } else {
            throw new Exception("此用户名已经被注册");
        }
    }

    private boolean isEmpty(String name) {
        QueryWrapper<DataBase> uqw = new QueryWrapper<>();
        uqw.eq("name", name);
        return dataBaseMapper.selectCount(uqw) == 0;
    }

    //添加一条数据
    public int add(DataBase dataBase) {
        //创建人
        String userName = SessionUtil.getLoginUserInfo().getUsername();
        dataBase.setCreateUser(userName);
        //创建时间
        dataBase.setCreateTime(new Date());
        return dataBaseMapper.insert(dataBase);
    }

    //添加多条数据
    public void add(List<DataBase> dataBaseList) {
        for (DataBase dataBase  : dataBaseList) {
            add(dataBase);
        }
    }

    //查询全部
    public List<DataBase> queryAll() {
        return dataBaseMapper.selectList(null);
    }

    //通过Id查询
    public DataBase queryById(DataBase dataBase) {
        return dataBaseMapper.selectById(dataBase.getId());
    }

    //通过Id查询
    public DataBase queryById(int id) {
        return dataBaseMapper.selectById(id);
    }

    //通过Id查询批量查询
    public List<DataBase> queryByIds() {
        List<Integer> idList = new ArrayList<>();
        idList.add(10);
        idList.add(11);
        return dataBaseMapper.selectBatchIds(idList);
    }

    //通过姓名模糊查询
    public List<DataBase> queryByName1(String name) {
        QueryWrapper<DataBase> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name", name); //参数为表中的列名，要查询的条件
        return dataBaseMapper.selectList(userQueryWrapper);
    }

    public List<DataBase> queryByNameMap(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        return dataBaseMapper.selectByMap(map);
    }

    //通过姓名精确查询
    public List<DataBase> queryByName2(String name) {
        QueryWrapper<DataBase> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name", name);
        return dataBaseMapper.selectList(userQueryWrapper);
    }

    // 计数
    public int count() {
        return dataBaseMapper.selectCount(null);
    }

    // 通过ID删除
    public int deleteById(DataBase dataBase) {
        return dataBaseMapper.deleteById(dataBase.getId());
    }

    // 通过ID删除
    public int deleteById(int id) {
        return dataBaseMapper.deleteById(id);
    }

    // 通过条件删除
    public void deleteBy(String column, Object val) {
        QueryWrapper<DataBase> userQueryWrapper = new QueryWrapper<>();

        userQueryWrapper.eq(column, val);
        int num = dataBaseMapper.delete(userQueryWrapper);
        System.out.println("影响行数：" + num);
    }

    public void delete(Map<String, Object> map) {
        dataBaseMapper.deleteByMap(map);
    }

    //通过id批量删除
    public void deleteByIds() {
        List<Integer> idList = new ArrayList<>();
        idList.add(10);
        idList.add(11);
        int num = dataBaseMapper.deleteBatchIds(idList);
        System.out.println("影响行数：" + num);
    }

    // 根据条件更新
    public void changeBy(DataBase dataBase, String column, Object val) {
        QueryWrapper<DataBase> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq(column, val);
        int num = dataBaseMapper.update(dataBase, userQueryWrapper);
        System.out.println("影响行数：" + num);
    }

    // 通过ID修改信息
    public void updateById(DataBase dataBase) {
        //修改人
        String userName = SessionUtil.getLoginUserInfo().getUsername();
        dataBase.setUpdateUser(userName);
        //修改时间
        dataBase.setUpdateTime(new Date());
        int num = dataBaseMapper.updateById(dataBase);
        System.out.println("影响行数：" + num);
    }
}
